package com.bw.packcar

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewTreeObserver
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.ApiConst
import com.bw.base.constants.RoutePath
import com.bw.base.constants.UserConst
import com.bw.common.view.BaseMvvmActivity
import com.bw.packcar.adapter.PopStringAdapter
import com.bw.packcar.databinding.ActivityUpdatePackCarBinding
import com.bw.packcar.model.PackEntity
import com.bw.packcar.net.CarPackViewModel
import com.bw.packcar.utils.PopUtils
import com.bw.packcar.utils.getStateType
import com.bw.packcar.utils.getStateTypeId
import com.tencent.mmkv.MMKV

/**
 * 智慧园区-车位管理
 * 编辑车位
 * 兰宏宇
 * 20250512
 */
@Route(path = RoutePath.PATH_PACK_CAR_UPDATE)
class UpdatePackCarActivity : BaseMvvmActivity<ActivityUpdatePackCarBinding, CarPackViewModel>() {



    @Autowired
    lateinit var packEntity: PackEntity

    private lateinit var spaceTypeAdapter: PopStringAdapter
    private lateinit var popSpaceType:PopUtils

    private lateinit var areaNameAdapter: PopStringAdapter
    private lateinit var popAreaName:PopUtils

    override fun initView() {
        binding.topBar.tvTitle.text = "编辑车位"
        binding.topBar.ivYes.visibility = View.VISIBLE
        binding.topBar.ivBack.setOnClickListener {
            finish()
        }

        binding.etRemarks.setText(packEntity.remarks)
        binding.etAreaName.setText(packEntity.areaName)
        binding.etUsername.setText(packEntity.userName)
        binding.etLicensePlate.setText(packEntity.licensePlate)
        binding.etSpaceType.setText(packEntity.spaceType)
        binding.etSpaceNumber.setText(packEntity.spaceNumber)
        binding.rgState.check(getStateTypeId(packEntity.spaceStatus))

        spaceTypeAdapter = PopStringAdapter()
        popSpaceType = PopUtils(this,binding.llSpaceType)
        popSpaceType.binding.rvPopString.adapter = spaceTypeAdapter
        popSpaceType.binding.rvPopString.layoutManager = LinearLayoutManager(this)

        areaNameAdapter = PopStringAdapter()
        popAreaName = PopUtils(this,binding.llAreaName)
        popAreaName.binding.rvPopString.adapter = areaNameAdapter
        popAreaName.binding.rvPopString.layoutManager = LinearLayoutManager(this)





        binding.ivSpaceTypeXl.setOnClickListener {
            popSpaceType.show()
        }


        binding.ivAreaNameXl.setOnClickListener {
            popAreaName.show()
        }

        vm.getSpaceType()
        vm.getAreaName()


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        super.onCreate(savedInstanceState)
    }

    override fun initData() {


        binding.btnSave.setOnClickListener {
            packEntity.spaceStatus = getState(binding.rgState.checkedRadioButtonId)
            packEntity.spaceType = binding.etSpaceType.text.toString()
            packEntity.areaName = binding.etAreaName.text.toString()
            packEntity.spaceNumber = binding.etSpaceNumber.text.toString()
            packEntity.remarks = binding.etRemarks.text.toString()
            packEntity.licensePlate = binding.etLicensePlate.text.toString()
            packEntity.userName = binding.etUsername.text.toString()

            if (packEntity.spaceType == "" ||
                packEntity.areaName == "" ||
                packEntity.spaceNumber == "" ||
                packEntity.remarks == "" ||
                packEntity.licensePlate == "" ||
                packEntity.userName == ""
            ) {
                ToastUtils.showShort("请先输入内容")
                return@setOnClickListener
            }

            val uid = MMKV.defaultMMKV().decodeInt(UserConst.USER_ID,0)
            if(uid==0){
                ToastUtils.showShort("请先登录")
                ARouter.getInstance().build(RoutePath.PATH_LOGIN).navigation()
                return@setOnClickListener
            }

            packEntity.areaId = 1
            packEntity.userId = uid
            vm.addCarPack(packEntity)
        }

        vm.addPackSuccess.observe(this){
            ToastUtils.showShort("编辑成功")
            finish()
        }

        vm.onFailed.observe(this){
            ToastUtils.showShort("新增失败")
        }

        vm.spaceTypeSuccess.observe(this){
            spaceTypeAdapter.data.clear()
            spaceTypeAdapter.data.addAll(it)
            spaceTypeAdapter.notifyDataSetChanged()
        }

        vm.areaNameSuccess.observe(this){
            areaNameAdapter.data.clear()
            areaNameAdapter.data.addAll(it)
            areaNameAdapter.notifyDataSetChanged()
        }

        spaceTypeAdapter.setOnItemClickListener { adapter, view, position ->
            binding.etSpaceType.setText( spaceTypeAdapter.data[position].description)
            popSpaceType.dismiss()
        }

        areaNameAdapter.setOnItemClickListener { adapter, view, position ->
            binding.etAreaName.setText( areaNameAdapter.data[position].description)
            popAreaName.dismiss()
        }


    }


    private fun getState(id: Int): String {
        var stateStr = "空闲"
        when (id) {
            binding.rgStateK.id -> {
                stateStr = "空闲"
            }
            binding.rgStateZ.id -> {
                stateStr = "已占用"
            }
            binding.rgStateW.id -> {
                stateStr = "维护中"
            }
        }
        return stateStr
    }

}